package org.jupiter.blog.service.service.impl;

import org.jupiter.blog.api.convertor.JupiterCategoryConvertor;
import org.jupiter.blog.api.domain.JupiterCategory;
import org.jupiter.blog.api.dto.JupiterCategoryDTO;
import org.jupiter.blog.service.mapper.JupiterCategoryMapper;
import org.jupiter.blog.service.service.JupiterCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class JupiterCategoryServiceImpl implements JupiterCategoryService {

	@Autowired
	private JupiterCategoryMapper categoryMapper;

	@Autowired
	private JupiterCategoryConvertor categoryConvertor;

	@Override
	public JupiterCategoryDTO getById(String uuid) {
		JupiterCategory category = categoryMapper.selectByUuid(uuid);
		return categoryConvertor.toDto(category);
	}

	@Override
	public List<JupiterCategoryDTO> getAll() {
		List<JupiterCategory> categories = categoryMapper.selectAll();
		return categories.stream()
				.map(categoryConvertor::toDto)
				.collect(Collectors.toList());
	}

	@Override
	public List<JupiterCategoryDTO> getByPage(JupiterCategoryDTO queryDTO) {
		int offset = (queryDTO.getPage() - 1) * queryDTO.getSize();
		List<JupiterCategory> categories = categoryMapper.selectByPage(
				queryDTO.getName(), offset, queryDTO.getSize()
		);
		return categories.stream()
				.map(categoryConvertor::toDto)
				.collect(Collectors.toList());
	}

	@Override
	public long getCount(JupiterCategoryDTO queryDTO) {
		return categoryMapper.countByQuery(queryDTO.getName());
	}

	@Override
	@Transactional
	public void create(JupiterCategoryDTO dto) {
		JupiterCategory category = categoryConvertor.toEntity(dto);
		if (!StringUtils.hasText(category.getUuid())){
			category.setUuid(UUID.randomUUID().toString());
		}
		category.setCreateTime(LocalDateTime.now());
		category.setUpdateTime(LocalDateTime.now());
		categoryMapper.insert(category);
	}

	@Override
	@Transactional
	public void update(JupiterCategoryDTO dto) {
		JupiterCategory category = categoryConvertor.toEntity(dto);
		category.setUuid(dto.getUuid());
		category.setUpdateTime(LocalDateTime.now());
		categoryMapper.updateByUuidSelective(category);
	}

	@Override
	@Transactional
	public void delete(String uuid) {
		categoryMapper.deleteByUuid(uuid);
	}

	@Override
	@Transactional
	public void updateSort(String uuid, Integer sort) {
		JupiterCategory category = new JupiterCategory();
		category.setUuid(uuid);
		category.setSort(sort);
		category.setUpdateTime(LocalDateTime.now());
		categoryMapper.updateByUuidSelective(category);
	}

	@Override
	public List<JupiterCategoryDTO> getActiveCategories() {
		List<JupiterCategory> categories = categoryMapper.selectActiveCategories();
		return categories.stream()
				.map(categoryConvertor::toDto)
				.collect(Collectors.toList());
	}
}
